package com.xy.iot.config;

import com.xy.iot.common.enums.CommandEnum;
import com.xy.iot.common.message.BaseMessage;
import com.xy.iot.common.result.message.JSONMessageData;
import com.xy.iot.common.result.message.ServerPingMessageData;
import com.xy.iot.config.properties.IotNettyServerProperties;
import com.xy.iot.config.properties.IotSerialPortProperties;
import com.xy.iot.netty.StartServer;
import com.xy.iot.netty.channel.BaseChannel;
import com.xy.iot.netty.listener.Listener;
import com.xy.iot.service.GnuSerialPortService;
import com.xy.iot.service.NettySerialPortRxtxClientService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;

/**
 * iot 服务启动配置
 */
@Slf4j
@Configuration
public class IotServerConfig implements ApplicationRunner {
	@Getter
	private static StartServer startServer = null;
	@Autowired
	private IotNettyServerProperties iotNettyServerProperties;
	@Autowired
	private IotSerialPortProperties iotSerialPortProperties;
	@Autowired
	private GnuSerialPortService gnuSerialPortService;
	@Autowired
	private NettySerialPortRxtxClientService nettyRxtxClientService;
	/**
	 * spring boot启动调用，启动 netty 服务
	 * @param args
	 * @throws Exception
	 */
	@Override
    public void run(ApplicationArguments args) throws Exception {
		if (iotNettyServerProperties.getOpen()){
			startServer = new StartServer(iotNettyServerProperties.getPort(), iotNettyServerProperties.getThread(), iotNettyServerProperties.getHeartBeatTime(), JSONMessageData.class);
			//添加dome监听案例
			startServer.addListener(CommandEnum.TEST_DOME.getKey(), new Listener() {
				@Override
				public void receive(BaseChannel channel, BaseMessage message) {
					log.info("{}业务，收到消息：{}",CommandEnum.TEST_DOME.getValue(), message);
				}
			});
			startServer.addListener(CommandEnum.CLIENT_PING.getKey(), new Listener() {
				@Override
				public void receive(BaseChannel channel, BaseMessage message) {
					log.info("{}业务，收到消息：{}",CommandEnum.CLIENT_PING.getValue(), message);
					channel.send(new ServerPingMessageData());
				}
			});
		}
		if (startServer!=null){
			startServer.start();
		}
		if (iotSerialPortProperties.getOpen()){
			if (iotSerialPortProperties.getNetty()){
				nettyRxtxClientService.init();
			}else {
				gnuSerialPortService.init();
			}
		}
	}

	/**
	 * spring boot 进程停止事件，关闭  netty 服务
	 * @param event
	 */
	@EventListener
	public void handleMyEvent(ContextClosedEvent event) {
		if (startServer!=null){
			startServer.stop();
		}

		if (iotSerialPortProperties.getOpen()){
			if (iotSerialPortProperties.getNetty()){
				nettyRxtxClientService.closeSerialPort();
			}else {
				gnuSerialPortService.closeSerialPort();
			}
		}
	}

}
